package org.client.chat;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.model.ChatStrings;

import sun.audio.AudioStream;

import com.cloudgarden.resource.SWTResourceManager;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public abstract class ITalkChatWindow {
	protected Shell shell;
	private Text sendTxt;
	private Button buzzBtn;
	private Button sendBtn;
	protected Text chatWindow;
	private Composite mainComposite;
	protected Display display;
	
	
	public static void main(String[] args) {
//		new ITalkChatWindow();
	}
		


	public ITalkChatWindow() {
		
		
		
		
		
		display = Display.getDefault();
		shell = new Shell(display,SWT.TITLE | SWT.MIN | SWT.CLOSE );
		//shell.setText(title);
		mainComposite = new Composite(shell, SWT.NULL);
		initGUI();
		mainComposite.setSize(320, 120);
		Point size = mainComposite.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		
		if(size.x == 0 && size.y == 0) {
			mainComposite.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y+50);
			shell.setSize(shellBounds.width, shellBounds.height+50);
		}
		shell.open();
		

		//shell.pack();
	}

	private void initGUI() {
		try {
			mainComposite.setBackground(SWTResourceManager.getColor(145, 147, 238));
			mainComposite.setLayout(new FormLayout());
			{
				buzzBtn = new Button(mainComposite, SWT.PUSH | SWT.CENTER);
				FormData button2LData = new FormData();
				button2LData.width = 30;
				button2LData.height = 23;
				button2LData.left =  new FormAttachment(0, 1000, 12);
				button2LData.top =  new FormAttachment(0, 1000, 132);
				buzzBtn.setLayoutData(button2LData);
				buzzBtn.addMouseListener(new MouseAdapter(){
					
					
					@Override
					public void mouseDown(MouseEvent e) {
						handleSend("Buzz");
					}
				});
				buzzBtn.setText("Bu");
			}
			{
				sendBtn = new Button(mainComposite, SWT.PUSH | SWT.CENTER);
				FormData button1LData = new FormData();
				button1LData.width = 50;
				button1LData.height = 47;
				button1LData.left =  new FormAttachment(0, 1000, 268);
				button1LData.top =  new FormAttachment(0, 1000, 160);
				sendBtn.setLayoutData(button1LData);
				sendBtn.setText("Send");
			}
			{
				chatWindow = new Text(mainComposite, SWT.MULTI | SWT.WRAP |SWT.V_SCROLL);
				chatWindow.setFont(SWTResourceManager.getFont("", 11, SWT.BOLD));
				FormData text2LData = new FormData();
				text2LData.width = 286;
				text2LData.height = 119;
				text2LData.left =  new FormAttachment(0, 1000, 7);
				text2LData.top =  new FormAttachment(0, 1000, 12);
				chatWindow.setLayoutData(text2LData);
			}
			{
				sendTxt = new Text(mainComposite, SWT.MULTI | SWT.WRAP |SWT.V_SCROLL);
				sendTxt.setFont(SWTResourceManager.getFont("", 11, SWT.BOLD));
				FormData text1LData = new FormData();
				text1LData.width = 232;
				text1LData.height = 47;
				text1LData.left =  new FormAttachment(0, 1000, 7);
				text1LData.top =  new FormAttachment(0, 1000, 160);
				sendBtn.addMouseListener(new MouseAdapter(){
					@Override
					public void mouseDown(MouseEvent e) {
						handleSend(sendTxt.getText());
					}
				});
				sendTxt.setLayoutData(text1LData);
			}
			mainComposite.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected abstract void handleSend(String str);
	protected void vibrate() {
		try {
			Point p = shell.getLocation();
			shell.setLocation(p.x, p.y+10);
			Thread.sleep(50);
			shell.setLocation(p.x+10, p.y);
			Thread.sleep(50);
			shell.setLocation(p.x+10, p.y+10);
			Thread.sleep(50);
			shell.setLocation(p.x, p.y);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	protected boolean checkIfEndChat(String line)
	{
		System.out.println("checking\n");
		if(line.length() == 3)
		{
			if(line.charAt(0) == 1 && line.charAt(1) == 2 && line.charAt(2) == 3)
			{
				chatWindow.append(ChatStrings.END_CHAT);
				//chatWindow.setCaretPosition(chatWindow.getText().length());
				return true;
			}
		}	
		return false;
	}
	
	protected AudioStream getBuzzAudioStream()
	{
		try {
			File file = new File("src\\org\\client\\chat\\buzz.wav");
			InputStream in = new FileInputStream(file.getAbsoluteFile() );
			return new AudioStream(in);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	protected AudioStream getMessageAudioStream()
	{
		try {
			File file = new File("src\\org\\client\\chat\\message.wav");
			InputStream in = new FileInputStream(file.getAbsoluteFile());
			return new AudioStream(in);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

}
